// Get the comment from the ls_comments table given the commentID
// and display to the swimmer or parent in a popup overlay.
function getComment(commentID) {
	// Get the ajax object.
	var xmlhttp = getAjaxObject();
	
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.getElementById("blackOverlay").style.display='block';
			// Display the comment box as an overlay that covers the page.
			document.getElementById("commentBox").style.display='block';
			// Position the popup layout so its centered in the browser
			var left = (getWindowWidth() - 620) / 2;
			document.getElementById("commentBox").style.left=left + "px";
			document.getElementById("commentBox").style.width='620px';
			document.getElementById("commentBox").style.height='300px';
			// Display the coaches name and the comment in the comment box that pops up.
			document.getElementById("commentText").innerHTML = xmlhttp.responseText;
		}
	}
	
	var data = "commentID=" + commentID;
	// Send the comment id to the php script which gets the coaches name
	// and the comment text to be displayed.
	xmlhttp.open("POST", "./ajax_coach_comments/get_coach_comments.php", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(data);
}

// Close the comment box by hiding it.
function closeBox() {
	document.getElementById("commentBox").style.display='none';
	document.getElementById("blackOverlay").style.display='none';
}

// Given an event id, show all swimmers that participated in that event to the manager.
function showEventSwimmers(eventID) {
	xmlhttp = getAjaxObject();
	
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			// Display the list of swimmers in a table.
			document.getElementById("commentContent").innerHTML = xmlhttp.responseText;
		}
	}
	
	var data = "eventID=" + eventID;
	// Send the event id to the php script which gets all swimmers
	// that participated in the event.
	xmlhttp.open("POST", "./ajax_coach_comments/get_event_swimmers.php", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(data);
}

// Create an ajax object and return it.
function getAjaxObject() {
	var xmlhttp = null;
	try {
		xmlhttp = new XMLHttpRequest();
	}
	catch(err) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(err) {
			alert("Error, your browser does not support AJAX");
		}
	}
	return xmlhttp;
}

// get window width
function getWindowWidth() {
	var viewportwidth;
	if(typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerWidth;
	}
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if(typeof document.documentElement != 'undefined'
			&& typeof document.documentElement.clientWidth !=
			'undefined' && document.documentElement.clientWidth != 0) {
		viewportwidth = document.documentElement.clientWidth;
	}
	// older versions of IE
	else {
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
	}
	return viewportwidth;
}